package com.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.entity.Course;
import com.edu.entity.CourseDescription;
import com.edu.entity.Teacher;
import com.edu.entity.frontvo.CourseFrontVo;
import com.edu.entity.frontvo.CourseWebVo;
import com.edu.entity.vo.CourseInfoVo;
import com.edu.entity.vo.CoursePublishVo;
import com.edu.exceptionhandler.MyException;
import com.edu.mapper.CourseMapper;
import com.edu.service.ChapterService;
import com.edu.service.CourseDescriptionService;
import com.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-17
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

	@Autowired
	private CourseDescriptionService courseDescriptionService;

	@Autowired
	private ChapterService chapterService;

	@Autowired
	private VideoService videoService;


	//添加课程信息的基本方法
	@Override
	public String saveCourseInfo(CourseInfoVo courseInfoVo) {

		//1 向课程表添加课程基本信息
		Course course = new Course();
		BeanUtils.copyProperties(courseInfoVo,course);
		int i = baseMapper.insert(course);

		if(i<=0){
			throw new MyException(20001,"添加课程失败");
		}

		//获取添加之后课程id
		String id = course.getId();

		//2 向课程简介表添加课程简介
		CourseDescription courseDescription = new CourseDescription();
		courseDescription.setDescription(courseInfoVo.getDescription());
		courseDescription.setId(id);
		courseDescriptionService.save(courseDescription);

		return id;
	}

	@Override
	public CourseInfoVo getCourseInfo(String courseId) {
		//1、查询课程表
		Course course = baseMapper.selectById(courseId);
		CourseInfoVo courseInfoVo = new CourseInfoVo();
		BeanUtils.copyProperties(course,courseInfoVo);

		//2、查询课程简介表
		CourseDescription description = courseDescriptionService.getById(courseId);
		courseInfoVo.setDescription(description.getDescription());

		return courseInfoVo;
	}

	@Override
	public void updateCourseInfo(CourseInfoVo courseInfoVo) {

		//1 修改课程表
		Course course = new Course();
		BeanUtils.copyProperties(courseInfoVo,course);
		int i = baseMapper.updateById(course);
		if (i == 0){
			throw new MyException(20001,"修改失败");
		}

		//2修改简介表
		CourseDescription description = new CourseDescription();
		description.setId(courseInfoVo.getId());
		description.setDescription(courseInfoVo.getDescription());
		courseDescriptionService.updateById(description);

	}

	@Override
	public CoursePublishVo getPublishCourseId(String id) {

		CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);

		return publishCourseInfo;
	}

	@Override
	public void removeCourse(String courseId) {

		//1 根据课程id删除小节
		videoService.removeVideoByCourseId(courseId);

		//2 根据课程id删除章节
		chapterService.removeChapterByCourseId(courseId);

		//3 根据课程id删除描述
		courseDescriptionService.removeById(courseId);

		//4 根据课程id删除课程本身
		int i = baseMapper.deleteById(courseId);
		if (i == 0){  //失败返回
			throw new MyException(20001,"删除失败");
		}
	}

	@Override
	public Map<String, Object> getFrontCourseList(Page<Course> pageParam, CourseFrontVo courseFrontVo) {
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		if (StringUtils.hasText(courseFrontVo.getSubjectParentId())){
			wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
		}
		if (StringUtils.hasText(courseFrontVo.getSubjectId())){
			wrapper.eq("subject_id",courseFrontVo.getSubjectId());
		}

		if (StringUtils.hasText(courseFrontVo.getBuyCountSort())){
			wrapper.orderByDesc("buy_count");
		}
		if (StringUtils.hasText(courseFrontVo.getGmtCreateSort())){
			wrapper.orderByDesc("buy_count");
		}
		if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
			wrapper.orderByDesc("price");
		}

		baseMapper.selectPage(pageParam,wrapper);

		List<Course> records = pageParam.getRecords();
		long current = pageParam.getCurrent();
		long pages = pageParam.getPages();
		long size = pageParam.getSize();
		long total = pageParam.getTotal();

		boolean hasNext = pageParam.hasNext();
		boolean hasPrevious = pageParam.hasPrevious();


		Map<String,Object> map = new HashMap<>();
		map.put("items", records);
		map.put("current", current);
		map.put("pages", pages);
		map.put("size", size);
		map.put("total", total);
		map.put("hasNext", hasNext);
		map.put("hasPrevious", hasPrevious);

		return map;
	}

	@Override
	public CourseWebVo getBaseCourseInfo(String courseId) {
		return baseMapper.getBaseCourseInfo(courseId);
	}


}
